import 'package:flutter/material.dart';
import 'package:kxy_flutter_app/model/exam_question.dart';
import 'package:kxy_flutter_app/view/examination/widget/option_type.dart';

class BlankWidget extends StatefulWidget {
  final AnswerOption option;
  final OptionType type;
  final bool canModified;
  final Function(String) onChange;

  const BlankWidget({
    Key key,
    @required this.option,
    @required this.type,
    this.canModified = true,
    this.onChange,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _BlankWidgetState();
  }
}

class _BlankWidgetState extends State<BlankWidget> {
  TextEditingController ctrl;

  @override
  void initState() {
    super.initState();
    ctrl = new TextEditingController(text: widget.option.option);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10, vertical: 8),
      margin: EdgeInsets.only(top: 20),
      decoration: BoxDecoration(
        color: bgColor(),
        border: Border.all(color: borderColor()),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Row(
        children: [
          dot(),
          Expanded(child: widget.canModified ? input(context) : label()),
          if (widget.type == OptionType.successAndChecked)
            Icon(
              Icons.check,
              color: labelColor(),
            ),
          if (widget.type == OptionType.error)
            Icon(
              Icons.close,
              color: labelColor(),
            ),
        ],
      ),
    );
  }

  Widget dot() {
    return Container(
      padding: EdgeInsets.all(6),
      alignment: Alignment.center,
      decoration: BoxDecoration(
        color: dotBgColor(),
        border: Border.all(color: dotBorderColor()),
        shape: BoxShape.circle,
      ),
      child: Text(
        widget.option.key,
        style: TextStyle(
          color: widget.type == OptionType.noraml ? dotBorderColor() : Colors.white,
        ),
      ),
    );
  }

  Widget input(BuildContext context) {
    return Container(
      color: Color.fromRGBO(240, 240, 240, 1),
      child: ConstrainedBox(
        constraints: BoxConstraints(
          maxHeight: 24,
          maxWidth: MediaQuery.of(context).size.width,
        ),
        child: TextField(
          cursorWidth: 1.5,
          controller: ctrl,
          onChanged: (val) => widget.onChange(val),
          decoration: InputDecoration(
            filled: true,
            hintText: '请输入答案',
            hintStyle: TextStyle(color: Color.fromRGBO(210, 210, 210, 1)),
            fillColor: bgColor(),
            contentPadding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
            border: OutlineInputBorder(borderSide: BorderSide.none),
          ),
        ),
      ),
    );
  }

  Widget label() {
    return Padding(
      padding: EdgeInsets.only(left: 10),
      child: Text(
        widget.option.option,
        softWrap: true,
        style: TextStyle(
          color: labelColor(),
        ),
      ),
    );
  }

  Color labelColor() {
    switch (widget.type) {
      case OptionType.noraml:
        return Color.fromRGBO(68, 68, 68, 1);
      case OptionType.primary:
        return Color.fromRGBO(12, 120, 242, 1);
      case OptionType.success:
      case OptionType.successAndChecked:
        return Color.fromRGBO(50, 199, 124, 1);
      case OptionType.error:
        return Color.fromRGBO(255, 115, 97, 1);
      default:
        return Color.fromRGBO(68, 68, 68, 1);
    }
  }

  Color dotBorderColor() {
    switch (widget.type) {
      case OptionType.noraml:
        return Color.fromRGBO(153, 153, 153, 1);
      case OptionType.primary:
        return Color.fromRGBO(38, 139, 255, 1);
      case OptionType.success:
      case OptionType.successAndChecked:
        return Color.fromRGBO(82, 204, 141, 1);
      case OptionType.error:
        return Color.fromRGBO(255, 154, 140, 1);
      default:
        return Color.fromRGBO(153, 153, 153, 1);
    }
  }

  Color dotBgColor() {
    switch (widget.type) {
      case OptionType.noraml:
        return Color.fromRGBO(250, 250, 250, 1);
      case OptionType.primary:
        return Color.fromRGBO(77, 160, 255, 1);
      case OptionType.success:
      case OptionType.successAndChecked:
        return Color.fromRGBO(102, 211, 159, 1);
      case OptionType.error:
        return Color.fromRGBO(255, 115, 97, 1);
      default:
        return Color.fromRGBO(250, 250, 250, 1);
    }
  }

  Color bgColor() {
    switch (widget.type) {
      case OptionType.noraml:
        return Color.fromRGBO(250, 250, 250, 1);
      case OptionType.primary:
        return Color.fromRGBO(237, 243, 250, 1);
      case OptionType.success:
      case OptionType.successAndChecked:
        return Color.fromRGBO(240, 250, 245, 1);
      case OptionType.error:
        return Color.fromRGBO(255, 244, 242, 1);
      default:
        return Color.fromRGBO(250, 250, 250, 1);
    }
  }

  Color borderColor() {
    switch (widget.type) {
      case OptionType.noraml:
        return Color.fromRGBO(224, 224, 224, 1);
      case OptionType.primary:
        return Color.fromRGBO(77, 160, 255, 1);
      case OptionType.success:
      case OptionType.successAndChecked:
        return Color.fromRGBO(82, 204, 141, 1);
      case OptionType.error:
        return Color.fromRGBO(255, 154, 140, 1);
      default:
        return Color.fromRGBO(224, 224, 224, 1);
    }
  }
}
